<?php
namespace frontend\controllers;

use common\models\Category;
use common\models\CategoryGroup;
use common\models\Comment;
use common\models\User;
use common\WuMing;
use frontend\models\UploadForm;
use frontend\models\UserInfoForm;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use common\models\Post;
use yii\web\UploadedFile;
use yii\filters\AccessControl;


/**
 * Logincontroller
 */
class MemberController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],

        ];
    }

    public function actionIndex()
    {
        $this->layout="member";
        $manage = WuMing::getManagerCategory(Yii::$app->user->id);
        return $this->render('index', [
            'manage' => $manage,
        ]);
    }

    /**收藏文章
     * @return string
     */
    public function actionCollect()
    {
        $this->layout="member";
        $query = Post::find()
            ->innerJoinWith('userPosts')
            ->innerJoinWith("cate")
            ->where(['user_post.user_id' => Yii::$app->user->id]);
        $countQuery = clone $query;
        $pages = new Pagination([
            'totalCount' => $countQuery->count(),
            'defaultPageSize' => 10
        ]);
        $model = $query
            ->offset($pages->offset)
            ->limit($pages->limit)
            ->orderBy(['user_post.time' => SORT_ASC])
            ->asArray()
            ->all();
        return $this->render('collect', [
            'model' => $model,
            'pages' => $pages
        ]);
    }
    /**用户发布过的文章
     * @return string
     */
    public function actionPost($type=null)
    {
        $this->layout="member";
        if($type ==1){
            $query = Post::find()
                ->innerJoinWith("cate")
                ->where(['post.user_id' => Yii::$app->user->id, 'post.status' => 1,'post.type' => 1]);
            $msg ="我发表的文章";
        }elseif($type ==2){
            $query = Post::find()
                ->innerJoinWith("cate")
                ->where(['post.user_id' => Yii::$app->user->id,  'post.status' => 1,'post.type' => 2]);
            $msg ="我发表的图片";
        }elseif($type ==2){
            $query = Post::find()
                ->innerJoinWith("cate")
                ->where(['post.user_id' => Yii::$app->user->id, 'post.status' => 1, 'post.type' => 3]);
            $msg ="我发表的链接";
        }else{
             $query = Post::find()
                ->innerJoinWith("cate")
                ->where(['post.user_id' => Yii::$app->user->id,'post.status' => 1]);
            $msg ="我的发表链接、话题和图片";
        }

        $countQuery = clone $query;
        $pages = new Pagination([
            'totalCount' => $countQuery->count(),
            'defaultPageSize' => 10
        ]);
        $model = $query
            ->offset($pages->offset)
            ->limit($pages->limit)
            ->orderBy(['post.create_at' => SORT_ASC])
            ->asArray()
            ->all();
       // WuMing::dd($model);die;
        return $this->render('post', [
            'model' => $model,
            'pages' => $pages,
            'msg' => $msg,
            'count' => $countQuery->count()
        ]);
    }


    /**用户信息
     * @return string
     */
    public function actionInfo()
    {
        $this->layout="member";
        $model = User::findOne(Yii::$app->user->id);
        $form = new UserInfoForm();
        $user = User::findOne(Yii::$app->user->id);
        $upload = new UploadForm();

        if ($form->load(Yii::$app->request->post())) {
            if ($form->updateInfo()) {
                return $this->render('info', [
                    'model' => $model,
                    'info' => $form,
                    'user' => $user,
                    'upload' => $upload,
                ]);
            }
        }



        if ($upload->load(Yii::$app->request->post())) {
          //  $user = User::findOne(Yii::$app->user->id);
            $thumb = UploadedFile::getInstance($upload, 'file');
            if ($thumb) {
                $dir = 'uploads/' . date("Ym");
                if (!file_exists($dir)) {
                    mkdir($dir);
                }
                $file_path = $dir . "/" . time() . rand(0, 99) . "." . $thumb->getExtension();
                $thumb->saveAs($file_path);
                $user->img = $file_path;
                if ($user->save()) {
                    return $this->render('info', [
                        'model' => $model,
                        'info' => $form,
                        'user' => $user,
                        'upload' => $upload,
                    ]);
                }
            }
        }
//        return $this->render('portrait', [
//
//        ]);



        return $this->render('info', [
            'model' => $model,
            'info' => $form,
            'user' => $user,
            'upload' => $upload,
        ]);
    }

    /**修改头像
     * @return string
     */
    public function actionPortrait()
    {
        $this->layout="member";
        $user = User::findOne(Yii::$app->user->id);
        $upload = new UploadForm();
        if ($upload->load(Yii::$app->request->post())) {
            $user = User::findOne(Yii::$app->user->id);
            $thumb = UploadedFile::getInstance($upload, 'file');
            if ($thumb) {
                $dir = 'uploads/' . date("Ym");
                if (!file_exists($dir)) {
                    mkdir($dir);
                }
                $file_path = $dir . "/" . time() . rand(0, 99) . "." . $thumb->getExtension();
                $thumb->saveAs($file_path);
                $user->img = $file_path;
                if ($user->save()) {
                    return $this->render('portrait', [
                        'user' => $user,
                        'upload' => $upload,
                    ]);
                }
            }
        }
        return $this->render('portrait', [
            'user' => $user,
            'upload' => $upload,
        ]);

    }
    public function actionComment()
    {
        $this->layout="member";
        $comment = Comment::find()
            ->where(['user_id'=>Yii::$app->user->id])
            ->orderBy(['id'=>SORT_DESC]);

        $pages = new Pagination([
            'totalCount' => $comment->count(),
            'defaultPageSize' => 10
        ]);
        $model = $comment->offset($pages->offset)->limit($pages->limit)->asArray()->all();
        return $this->render('comment',[
            'model'=>$model,
            'pages'=>$pages,
        ]);

    }


    public function actionMessage()
    {
        $this->layout="member";
        $comment = Comment::find()
            ->where(['reply_to'=>Yii::$app->user->id])
            ->orderBy(['id'=>SORT_DESC]);
        $c = clone $comment;
        $comment = $comment->innerJoinWith("post");
        $pages = new Pagination([
            'totalCount' => $c->count(),
            'defaultPageSize' => 10
        ]);
        $model = $comment->offset($pages->offset)->limit($pages->limit)->asArray()->all();
     //   WuMing::dd($model);die;
        return $this->render('message',[
            'model'=>$model,
            'pages'=>$pages,
        ]);

    }






}
